using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NCS.Library.Components;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library;
using NCS.Module.CMS.Library.Definitions;
using NCS.Module.CMS.Library.Zones;
using NCS.Library;

namespace NCS.Module.CMS.Web.HomePage
{
    public partial class NavRight : NCS.Module.CMS.Library.CMSWebControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = ZoneController.GetZonesVisbleInLeftBottom();
                this.rpt_foot.DataSource = dt;
                this.rpt_foot.ItemDataBound += new RepeaterItemEventHandler(rpt_foot_ItemDataBound);
                this.rpt_foot.DataBind();
            }
        }

        void rpt_foot_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView row = (DataRowView)e.Item.DataItem;
            if (row != null)
            {
                HyperLink link = (HyperLink)e.Item.FindControl("hplk");

                int zone_authorize = ConvertUtility.ToInt32(row["Zone_Authorize"].ToString(), 0);
                if (zone_authorize > 0)
                {
                    if (!this.Request.IsAuthenticated)
                    {
                        e.Item.Visible = false;
                        return;
                    }
                    else
                    {

                        if (this.CurrentUser.UserType == 0 && zone_authorize > 1)
                        {
                            e.Item.Visible = false;
                            return;
                        }
                    }
                }

                int zoneid = NCS.Library.Utilities.ConvertUtility.ToInt32(row["Zone_ID"].ToString());
                int tabid = NCS.Library.Utilities.ConvertUtility.ToInt32(row["Portal_TabControlID"].ToString());
                int authorize = NCS.Library.Utilities.ConvertUtility.ToInt32(row["Zone_Authorize"], 0);
                if (zoneid != 0 && ((authorize == 0 && this.Request.IsAuthenticated == false) || (authorize <= 1 && this.CurrentUser.UserType == 0) || (authorize <= 2 && this.CurrentUser.UserType == 1)))
                {
                    if (row["Zone_Url"] != null)
                        link.NavigateUrl = row["Zone_Url"].ToString();
                    if (link.NavigateUrl.Length == 0)
                        link.NavigateUrl = BuildUrlByZoneSubPortal(AppEnv.PortalId(), zoneid.ToString(), tabid.ToString());
                    link.Text = row["Zone_Name"].ToString();

                    Image img = (Image)e.Item.FindControl("img");
                    img.ImageUrl = row["Zone_Avatar"].ToString();
                }
                else
                {
                    e.Item.Visible = false;
                    return;
                }
            }
        }
    }
}